package com.idbk.chargestation.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.charge.ActivityChargingAc;
import com.idbk.chargestation.bean.JsonPointPile;
import com.idbk.chargestation.bean.JsonQrScan;

import java.util.Locale;

public class FragmentChargeByElectricity extends Fragment implements OnClickListener {

    public View mView;
    private TextView mEighty, mForty, mTwenty, mTen, mOther, mBalance;
    private EditText mInputElect;
    private TextView[] mTextMoneyArrays = new TextView[5];
    private int mSelectElect = 80;
    public boolean isChoose = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_charge_electricity, container, false);

        setupView();

        setupData();

        return mView;
    }

    private void setupView() {
        mEighty = (TextView) mView.findViewById(R.id.hundred);
        mForty = (TextView) mView.findViewById(R.id.eighty);
        mTwenty = (TextView) mView.findViewById(R.id.fivty);
        mTen = (TextView) mView.findViewById(R.id.twenty);
        mBalance = (TextView) mView.findViewById(R.id.balance);
        mOther = (TextView) mView.findViewById(R.id.other);
        mInputElect = (EditText) mView.findViewById(R.id.money);
        mView.findViewById(R.id.start).setOnClickListener(this);

        mTextMoneyArrays[0] = mEighty;
        mTextMoneyArrays[1] = mForty;
        mTextMoneyArrays[2] = mTwenty;
        mTextMoneyArrays[3] = mTen;
        mTextMoneyArrays[4] = mOther;
        for (int i = 0; i < mTextMoneyArrays.length; i++) {
            mTextMoneyArrays[i].setOnClickListener(this);
        }
    }

    private void setupData() {
        JsonQrScan mPointInfor = getArguments().getParcelable(JsonQrScan.KEY);

        SpannableString spanEighty, spanForty, spanTwenty, spanTen;

        spanEighty = new SpannableString("80度");
        spanEighty.setSpan(new AbsoluteSizeSpan(14, true), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanEighty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_0)), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanEighty.setSpan(new AbsoluteSizeSpan(12, true), 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanEighty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_9)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mEighty.setText(spanEighty);

        spanForty = new SpannableString("40度");
        spanForty.setSpan(new AbsoluteSizeSpan(14, true), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanForty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_3)), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanForty.setSpan(new AbsoluteSizeSpan(10, true), 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanForty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_9)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mForty.setText(spanForty);

        spanTwenty = new SpannableString("20度");
        spanTwenty.setSpan(new AbsoluteSizeSpan(14, true), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanTwenty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_3)), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanTwenty.setSpan(new AbsoluteSizeSpan(10, true), 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanTwenty.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_9)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTwenty.setText(spanTwenty);

        spanTen = new SpannableString("10度");
        spanTen.setSpan(new AbsoluteSizeSpan(14, true), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanTen.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_3)), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanTen.setSpan(new AbsoluteSizeSpan(10, true), 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanTen.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getActivity(), R.color.tj_text_content_9)), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTen.setText(spanTen);

        if (mPointInfor != null) {
            mBalance.setText(String.format(Locale.CHINA, "%.2f", mPointInfor.balance));
        }
    }


    private void setMoneySelect(int index) {
        for (int i = 0; i < mTextMoneyArrays.length; i++) {
            if (i == index) {
                mTextMoneyArrays[i].setBackgroundResource(R.drawable.bg_select_box_active_sprite);
            } else {
                mTextMoneyArrays[i].setBackgroundResource(R.drawable.bg_select_box_active_disabled);
            }

            if (index == mTextMoneyArrays.length - 1) {
                mInputElect.setBackgroundResource(R.drawable.bg_textfield_normal_sprite);
                mInputElect.setFocusableInTouchMode(true);
                isChoose = true;

            } else {
                mInputElect.setFocusable(false);
                mInputElect.setText("");
                mInputElect.setBackgroundResource(R.drawable.bg_textfield_disabled_sprite);
                mSelectElect = 80;
                isChoose = false;
            }
        }
    }


    private boolean checkInput() {
        if (isChoose) {
            String money = mInputElect.getText().toString().trim();
            if (money.equals("") || money.equals("")) {
                Snackbar.make(mInputElect, "请输入正确的数值！", Snackbar.LENGTH_SHORT).show();
                return false;

            } else {
                int value = Integer.parseInt(money);
                if (value > 9999 || value < 0) {
                    Snackbar.make(mInputElect, "请输入0-9999的整数电量（度）！", Snackbar.LENGTH_SHORT).show();
                    return false;

                } else {
                    mSelectElect = value;
                    return true;
                }
            }
        }
        return true;
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.hundred:
                setMoneySelect(0);
                mSelectElect = 80;
                break;
            case R.id.eighty:
                setMoneySelect(1);
                mSelectElect = 40;
                break;
            case R.id.fivty:
                setMoneySelect(2);
                mSelectElect = 20;
                break;
            case R.id.twenty:
                setMoneySelect(3);
                mSelectElect = 10;
                break;
            case R.id.other:
                setMoneySelect(4);
                break;
            case R.id.start:
                if (checkInput()) {
                    System.out.println("mSelectElect-->" + mSelectElect);
                    Intent intent = getActivity().getIntent();
                    intent.putExtra("mMethod", JsonPointPile.CHARGE_FINISH_BY_ENERGY);
                    intent.putExtra("mSelect", mSelectElect);
                    intent.putExtra(ActivityChargingAc.KEY_CHARGE_ACTION, ActivityChargingAc.CHARGE_ACTION_WAIT);
                    getActivity().setResult(Activity.RESULT_OK, intent);
                    getActivity().finish();
                }
                break;
            default:
                break;
        }
    }

}
